fs.readdir   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
nc 3
dl 0
loc 39
rs 8.439
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 6 2
1
var fs = require('fs');
2
var pkijs = require('pkijs');
3
var asn1js = require('asn1js');
4
var Validation = require('../lib/x509/validation.pkijs');
5
var Certificate = pkijs.Certificate;
6
7
var path = "/etc/ssl/certs/";
8
9
fs.readdir(path, function(err, items) {
10
    var trustFile = {};
11
    for (var i = 0; i < items.length; i++) {
12
        if (items[i].slice(-4) === ".pem") {
13
            var pem = fs.readFileSync(path + items[i]).toString();
14
            pem = pem.replace(/-----BEGIN CERTIFICATE-----/g, '');
15
            pem = pem.replace(/-----END CERTIFICATE-----/g, '');
16
            pem = pem.replace(/\s+/g, '');
17
18
            try {
19
                var der = Validation.stringToArrayBuffer(Buffer.from(pem, 'base64'));
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
20
                var asn = asn1js.fromBER(der);
21
22
            } catch (e) {
23
                throw new Error("Failed to decode certificate")
24
            }
25
26
            if (asn.offset === -1) {
27
                throw new Error("Failed to decode certificate")
28
            }
29
30
            try {
31
                var cert = new Certificate({schema: asn.result});
32
            } catch (e) {
33
                throw new Error("Failed to decode certificate")
34
            }
35
36
            var subject;
37
            cert.subject.typesAndValues.map(function(typeAndValue) {
38
                if (typeAndValue.type.toLowerCase() === "2.5.4.3") {
39
                    subject = typeAndValue.value.valueBlock.value;
40
                    trustFile[""+subject] = pem;
0 ignored issues
show
Bug introduced by
The variable pem is changed as part of the for loop for example by pem.replace(\s+, "") on line 16. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
Bug introduced by
The variable subject is changed as part of the for loop for example by typeAndValue.value.valueBlock.value on line 39. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
41
                }
42
            });
43
        }
44
    }
45
46
    console.log(JSON.stringify(trustFile, null, 2));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
47
});
48